package com.changgou.seckill.controller;

import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.seckill.pojo.SeckillGoods;
import com.changgou.seckill.service.SecKillGoodsService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/seckillgoods")
public class SecKillGoodsController {

    @Autowired
    private SecKillGoodsService secKillGoodsService;


    /**
     * 分页查询活动商品
     * @param id
     * @param current
     * @param pageSize
     * @return
     */
    @GetMapping("/findById/{id}/{current}/{pageSize}")
    public PageResult findById(@PathVariable int id,@PathVariable int current,@PathVariable int pageSize){
        Page<SeckillGoods> pageById = secKillGoodsService.findById(id ,current,pageSize);
        return new PageResult(pageById.getTotal(),pageById.getResult());
    }


    /**
     * 分页查询所有商品
     * @param current
     * @param pageSize
     * @param searchMap
     * @return
     */
    @GetMapping("/findAllSeckillGoods/{current}/{pageSize}")
    public PageResult findAllSeckillGoods(@PathVariable int current, @PathVariable int pageSize, @RequestParam Map<String,String > searchMap){
        Page<SeckillGoods> allSeckillGoods = secKillGoodsService.findAllSeckillGoods(current , pageSize ,searchMap);
        return new PageResult(allSeckillGoods.getTotal(),allSeckillGoods.getResult());
    }

    /**
     * 更新商品秒杀对应的数据——价格、数量、限购数量、排序
     * @param seckillGoods
     * @return
     */
    @PutMapping
    public Result updateGoods(@RequestBody SeckillGoods seckillGoods){
        secKillGoodsService.updateGoods(seckillGoods);
        return new Result(true,StatusCode.OK,"修改成功");
    }


}
